package com.econ.powercloud.ems.service.dao;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.econ.powercloud.ems.common.enums.DeviceType;
import com.econ.powercloud.ems.dal.entity.EmsDevice;
import com.econ.powercloud.ems.dal.mapper.EmsDeviceMapper;
import com.mysql.cj.util.StringUtils;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Repository;

import java.util.Arrays;
import java.util.List;

/**
 * @author zjr
 * @create 2021/7/22 下午2:31
 * @description
 */
@Repository
public class EmsDeviceDAO extends ServiceImpl<EmsDeviceMapper, EmsDevice> {

    public EmsDevice selectByDeviceCode(String deviceCode) {
        LambdaQueryWrapper<EmsDevice> emsDeviceLambdaQueryWrapper = Wrappers.lambdaQuery();
        emsDeviceLambdaQueryWrapper.eq(!StringUtils.isNullOrEmpty(deviceCode), EmsDevice::getDeviceCode, deviceCode);
        return this.baseMapper.selectOne(emsDeviceLambdaQueryWrapper);
    }

    public Page<EmsDevice> selectPageByDeviceName(String name,Integer pageNo,Integer pageSize) {
        LambdaQueryWrapper<EmsDevice> wrapper = Wrappers.lambdaQuery();
        wrapper.like(!StringUtils.isNullOrEmpty(name), EmsDevice::getDeviceName, name);
        Page<EmsDevice> page = new Page<>(pageNo, pageSize);
        return this.baseMapper.selectPage(page,wrapper);
    }

    public List<EmsDevice> selectByDeviceCodes(List<String> deviceCodes) {
        LambdaQueryWrapper<EmsDevice> emsDeviceLambdaQueryWrapper = Wrappers.lambdaQuery();
        emsDeviceLambdaQueryWrapper.in(CollectionUtils.isNotEmpty(deviceCodes), EmsDevice::getDeviceCode, deviceCodes);
        return this.baseMapper.selectList(emsDeviceLambdaQueryWrapper);
    }

    public List<EmsDevice> selectByEnergyStationId(String energyStationId) {
        LambdaQueryWrapper<EmsDevice> emsDeviceLambdaQueryWrapper = Wrappers.lambdaQuery();
        emsDeviceLambdaQueryWrapper.eq(!StringUtils.isNullOrEmpty(energyStationId), EmsDevice::getEnergyStationId, energyStationId);
        return this.baseMapper.selectList(emsDeviceLambdaQueryWrapper);
    }

    /**
     * 通过设备code获取子设备
     */
    public List<EmsDevice> selectChildByDeviceCodes(List<String> deviceCodes){
        LambdaQueryWrapper<EmsDevice> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.in(EmsDevice::getParentDeviceCode,deviceCodes);
        return this.baseMapper.selectList(lambdaQueryWrapper);
    }

    /**
     * 获取一个储能装置与储能站的PCS根设备
     * @param energyStationId: 储能站id
     * @param energyEquipmentId: 储能装置id
     */
    public EmsDevice getPCSByStationIdAndEquipmentId(String energyStationId, String energyEquipmentId){
        LambdaQueryWrapper<EmsDevice> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(EmsDevice::getEnergyStationId,energyStationId);
        lambdaQueryWrapper.eq(EmsDevice::getEnergyEquipmentId,energyEquipmentId);
        lambdaQueryWrapper.eq(EmsDevice::getDeviceType, DeviceType.PCS.getCode());
        lambdaQueryWrapper.last("limit 1");
        return this.baseMapper.selectOne(lambdaQueryWrapper);
    }

    /**
     * 获取储能站id,储能装置id,设备类型获取对应的数据（都不传默认查全部）
     * @param energyStationId: 储能站id(非必填)
     * @param energyEquipmentId: 储能装置id(非必填)
     * @param deviceType: 设备类型（见枚举,非必填）
     */
    public List<EmsDevice> getListByCondition(String energyStationId, String energyEquipmentId,
                                              Integer deviceType){
        LambdaQueryWrapper<EmsDevice> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(!StringUtils.isNullOrEmpty(energyStationId), EmsDevice::getEnergyStationId,energyStationId);
        lambdaQueryWrapper.eq(!StringUtils.isNullOrEmpty(energyEquipmentId), EmsDevice::getEnergyEquipmentId,energyEquipmentId);
        lambdaQueryWrapper.eq(deviceType != null, EmsDevice::getDeviceType, deviceType);
        return this.baseMapper.selectList(lambdaQueryWrapper);
    }

    /**
     * 获取储能站id,储能装置id,设备类型集合对应的数据
     * @param energyStationId: 储能站id(非必填)
     * @param energyEquipmentId: 储能装置id(非必填)
     * @param deviceTypes: 设备类型集合（见枚举,非必填）
     */
    public List<EmsDevice> getListByStationIdAndEquipmentIdAndDeviceTypes(String energyStationId, String energyEquipmentId,
                                                                          List<Integer> deviceTypes){
        LambdaQueryWrapper<EmsDevice> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(!StringUtils.isNullOrEmpty(energyStationId), EmsDevice::getEnergyStationId,energyStationId);
        lambdaQueryWrapper.eq(!StringUtils.isNullOrEmpty(energyEquipmentId), EmsDevice::getEnergyEquipmentId,energyEquipmentId);
        lambdaQueryWrapper.in(CollectionUtils.isNotEmpty(deviceTypes), EmsDevice::getDeviceType, deviceTypes);
        return this.baseMapper.selectList(lambdaQueryWrapper);
    }

    /**
     * 分页获取储能站id,储能装置id,设备类型集合对应的数据
     * @param energyStationId: 储能站id(非必填)
     * @param energyEquipmentId: 储能装置id(非必填)
     * @param deviceTypes: 设备类型集合（见枚举,非必填）
     * @param pageNo
     * @param pageSize
     */
    public Page<EmsDevice> getPageByStationIdAndEquipmentIdAndDeviceTypes(String energyStationId, String energyEquipmentId,
                                                                          List<Integer> deviceTypes, Integer pageNo, Integer pageSize){
        Page<EmsDevice> emsDevicePage = new Page<>(pageNo, pageSize);
        LambdaQueryWrapper<EmsDevice> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(!StringUtils.isNullOrEmpty(energyStationId), EmsDevice::getEnergyStationId,energyStationId);
        lambdaQueryWrapper.eq(!StringUtils.isNullOrEmpty(energyEquipmentId), EmsDevice::getEnergyEquipmentId,energyEquipmentId);
        lambdaQueryWrapper.in(CollectionUtils.isNotEmpty(deviceTypes), EmsDevice::getDeviceType, deviceTypes);
        return this.baseMapper.selectPage(emsDevicePage,lambdaQueryWrapper);
    }

    /**
     * 分页获取父设备 与 子设备类型对应的数据
     * @param deviceCode: 父设备code
     * @param deviceType: 子设备类型
     * @param pageNo
     * @param pageSize
     */
    public Page<EmsDevice> getPageChildDeviceByCondition(String deviceCode, Integer deviceType, Integer pageNo, Integer pageSize){
        Page<EmsDevice> emsDevicePage = new Page<>(pageNo, pageSize);
        LambdaQueryWrapper<EmsDevice> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(!StringUtils.isNullOrEmpty(deviceCode), EmsDevice::getParentDeviceCode,deviceCode);
        lambdaQueryWrapper.eq(deviceType != null, EmsDevice::getDeviceType, deviceType);
        return this.baseMapper.selectPage(emsDevicePage,lambdaQueryWrapper);
    }

    public List<EmsDevice> getChildDeviceByCondition(String deviceCode, Integer deviceType){
        LambdaQueryWrapper<EmsDevice> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(!StringUtils.isNullOrEmpty(deviceCode), EmsDevice::getParentDeviceCode,deviceCode);
        lambdaQueryWrapper.eq(deviceType != null, EmsDevice::getDeviceType, deviceType);
        return this.baseMapper.selectList(lambdaQueryWrapper);
    }

    public int deleteByStationId(String stationId){
        LambdaQueryWrapper<EmsDevice> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(EmsDevice::getEnergyStationId,stationId);
        return this.baseMapper.delete(lambdaQueryWrapper);
    }

    public Page<EmsDevice> getPageBySearch(String search, String energyStationId, String energyEquipmentId, Integer pageNo, Integer pageSize) {
        Page<EmsDevice> emsDevicePage = new Page<>(pageNo, pageSize);
        LambdaQueryWrapper<EmsDevice> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.like(!StringUtils.isNullOrEmpty(search), EmsDevice::getDeviceName,search);
        lambdaQueryWrapper.eq(energyEquipmentId!=null, EmsDevice::getEnergyEquipmentId,energyEquipmentId);
        lambdaQueryWrapper.eq(energyStationId!=null, EmsDevice::getEnergyStationId,energyStationId);
        return this.baseMapper.selectPage(emsDevicePage,lambdaQueryWrapper);
    }

    public List<EmsDevice> selectByStationIdsAndDeviceTypes(List<String> stationIds, List<Integer> deviceTyps){
        LambdaQueryWrapper<EmsDevice> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.in(CollectionUtils.isNotEmpty(stationIds), EmsDevice::getEnergyStationId,stationIds);
        lambdaQueryWrapper.in(CollectionUtils.isNotEmpty(deviceTyps), EmsDevice::getDeviceType, deviceTyps);
        return this.baseMapper.selectList(lambdaQueryWrapper);
    }

    public List<EmsDevice> selectByDeviceCodeAndEquipmentId(String deviceCode, String energyEquipmentId) {
        LambdaQueryWrapper<EmsDevice> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(org.apache.commons.lang3.StringUtils.isNotEmpty(deviceCode), EmsDevice::getDeviceCode, deviceCode);
        wrapper.eq(org.apache.commons.lang3.StringUtils.isNotEmpty(energyEquipmentId), EmsDevice::getEnergyEquipmentId, energyEquipmentId);
        wrapper.eq(EmsDevice::getModuleCode, "def");
        return this.baseMapper.selectList(wrapper);
    }

    public List<EmsDevice> selectByModuleCode() {
        LambdaQueryWrapper<EmsDevice> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(EmsDevice::getModuleCode, "def");
        return this.baseMapper.selectList(wrapper);
    }

    public List<EmsDevice> selectByInitDeviceCodes(List<String> initDeviceCodes) {
        LambdaQueryWrapper<EmsDevice> emsDeviceLambdaQueryWrapper = Wrappers.lambdaQuery();
        emsDeviceLambdaQueryWrapper.in(CollectionUtils.isNotEmpty(initDeviceCodes), EmsDevice::getInitDeviceCode, initDeviceCodes);
        return this.baseMapper.selectList(emsDeviceLambdaQueryWrapper);
    }

    public List<EmsDevice> getListByParentId(String parentDeviceCode) {
        LambdaQueryWrapper<EmsDevice> emsDeviceLambdaQueryWrapper = Wrappers.lambdaQuery();
        emsDeviceLambdaQueryWrapper.eq(EmsDevice::getParentDeviceCode,parentDeviceCode);
        return this.baseMapper.selectList(emsDeviceLambdaQueryWrapper);
    }

    public EmsDevice selectByInitDeviceCodeAndModule(String initDeviceCode, String moduleCode) {
        LambdaQueryWrapper<EmsDevice> emsDeviceLambdaQueryWrapper = Wrappers.lambdaQuery();
        emsDeviceLambdaQueryWrapper.eq(EmsDevice::getInitDeviceCode,initDeviceCode);
        emsDeviceLambdaQueryWrapper.eq(EmsDevice::getModuleCode,moduleCode);
        return this.baseMapper.selectOne(emsDeviceLambdaQueryWrapper);
    }

    public List<EmsDevice> listByType(Integer deviceType) {
        LambdaQueryWrapper<EmsDevice> emsDeviceLambdaQueryWrapper = Wrappers.lambdaQuery();
        emsDeviceLambdaQueryWrapper.eq(EmsDevice::getDeviceType,deviceType);
        return this.baseMapper.selectList(emsDeviceLambdaQueryWrapper);
    }
}
